package com.kzd.gtgame.main.money

import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.money.adapter.MoneyTaskMallAdapter
import com.kzd.gtgame.main.toMineRecordBillActivity
import com.kzd.gtgame.main.toMoneyMallDetActivity
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_mall_list.*

class MoneyMallListActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMoneyTaskMallAdapter = MoneyTaskMallAdapter()

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_mall_list

    override fun stateBar(): StatusBar {
        return StatusBar(view = tlMoneyTaskMallBar)
    }

    override fun initView() {
        rvMoneyTaskMallBody.adapter = mMoneyTaskMallAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                toMoneyMallDetActivity(itemData.goods_id)
            }
        }

        //兑换记录
        tvMoneyTaskMallRecord.setOnClickAntiShakeListener {
            toMineRecordBillActivity(index = 2)
        }
    }

    override fun initData() {
        viewModel.moneyTaskMall()
    }

    override fun onResume() {
        super.onResume()
        viewModel.moneySign()
    }

    override fun initViewMode() {

        viewModel.moneySignLiveData.observe(this as LifecycleOwner, { response ->
            mslMultiStatusLayout.setLoadingStatus(response)
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            tvMoneyTaskMallPoint.text = data.points.toString()
        })

        viewModel.moneyTaskMallLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyTaskMallAdapter.setNewInstance(data)
        })
    }
}